#include "registerdialog.h"
#include "ui_registerdialog.h"
#include <logindialog.h>
#include <QMessageBox>
#include <QSqlQuery>
registerDialog::registerDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::registerDialog)
{
    ui->setupUi(this);

    QPixmap pix1 (":/myimages/1.jpg");
    ui->label_6->setPixmap(pix1);
    QPixmap pix2 (":/myimages/3.jpg");
    ui->label_4->setPixmap(pix2);
    ui->rpasswordlineEdit->setEchoMode(QLineEdit::Password);
     ui->cpasswordlineEdit->setEchoMode(QLineEdit::Password);

}


registerDialog::~registerDialog()
{
    delete ui;
}

void registerDialog::on_confirmButton_clicked()
{
    QString username = ui->ruserlineEdit->text();
    QString password = ui->rpasswordlineEdit->text();
    QString cpassword = ui->cpasswordlineEdit->text();
    if(username.isEmpty() || password.isEmpty() || cpassword.isEmpty()){
        QMessageBox::warning(this,"警告","账号或密码不能为空！");
        return;
    }
    if(password!=cpassword){
        QMessageBox::warning(this,"警告","两次输入密码不一样！");
        ui->cpasswordlineEdit->clear();
        return;
    }

    QSqlQuery query;
    query.prepare("SELECT * FROM users WHERE username =:username");
    query.bindValue(":username",username);
    query.exec();
    if(query.next()){
        QMessageBox::warning(this,"警告","该账号已经被注册！");
        return;
    }
    query.prepare("INSERT INTO users(username,password) VALUES(:username,:password)");
    query.bindValue(":username",username);
    query.bindValue(":password",password);
    if(!query.exec()){
        QMessageBox::warning(this,"警告","注册失败！");
        ui->ruserlineEdit->clear();
        ui->cpasswordlineEdit->clear();
        ui->rpasswordlineEdit->clear();
        return;
    }else{
        QMessageBox::information(this,"提示","注册成功");
        Dialog *w = new Dialog;
        w->show();
        this->hide();
    }



}


void registerDialog::on_returnButton_clicked()
{
    this->close();
    Dialog *a=new Dialog;
    a->show();

}

